package com.jadesoft.ciadapter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

import com.cj.trim.csstrimResponseWrapper;

public class CssFilter implements Filter {

	public void init(FilterConfig paramFilterConfig) throws ServletException {
	}

	public void destroy() {
	}

	public void doFilter(ServletRequest paramServletRequest,
			ServletResponse paramServletResponse, FilterChain paramFilterChain)
			throws IOException, ServletException {
		csstrimResponseWrapper cssResponseWrapper = new csstrimResponseWrapper((HttpServletResponse) paramServletResponse);
		paramFilterChain.doFilter(paramServletRequest, cssResponseWrapper);
		String css = new String(cssResponseWrapper.getData());
		String result = css;
		result = replaceCss(result);

		String contentType = cssResponseWrapper.getContentType();
		ServletOutputStream localServletOutputStream = null;
		if (contentType != null)
			paramServletResponse.setContentType(contentType);
		else
			paramServletResponse.setContentType("text/css");
		localServletOutputStream = paramServletResponse.getOutputStream();
		byte[] resultByte = result.getBytes();
		int len = resultByte.length;
		paramServletResponse.setContentLength(len);
		localServletOutputStream.write(resultByte, 0, len);
		if (localServletOutputStream != null) {
			localServletOutputStream.flush();
			localServletOutputStream.close();
		}
	}

	private String replaceCss(String css) {
		return css.replaceAll(
						"jd_linear_gradient\\(([^,]+?),([^,]+?)\\);?",
								  "background-color:$1;"
								+ "background-image: -moz-linear-gradient(top, $1,$2);"
								+ "background-image: -o-linear-gradient(top, $1, $2 50%,$1 99%,$1);"
								+ "background-image: -webkit-gradient(linear, left top, left bottom, from($1), to($2));"
								+ "filter: progid:DXImageTransform.Microsoft.gradient(startColorStr='$1',EndColorStr='$2');")
				.replaceAll(
						"jd_linear_gradient\\((.+?),(.+?),(.+?),(.+?)\\);?",
						"background-color:$3;"
								+ "background-image: -moz-linear-gradient(center top, $1,$2 45%,$3 45%,$4);"
								+ "background-image: -o-linear-gradient(top, $1, $2 45%,$3 45%,$1);"
								+ "background-image: -webkit-gradient(linear, left top, left bottom, "
								+ "from($1),color-stop(45%, $2),color-stop(45%,$3),  to($4));"
								+ "filter: progid:DXImageTransform.Microsoft.gradient(startColorStr='$1',EndColorStr='$3');")
				.replaceAll(
						"jd_border_radius\\((.+?),(.+?),(.+?),(.+?)\\);?",
						"-webkit-border-top-left-radius: $1;"
								+ "-webkit-border-top-right-radius: $2;"
								+ "-webkit-border-bottom-right-radius: $3;"
								+ "-webkit-border-bottom-left-radius: $4;"
								+ "-webkit-border-radius: $1 $2 $3 $4;"
								+ "-moz-border-radius: $1 $2 $3 $4;"
								+ "border-radius: $1 $2 $3 $4;").replaceAll(
						"jd_box_shade\\((.+?)\\);?",
						"box-shadow: $1 ;" + "-webkit-box-shadow: $1 ;"
								+ "-moz-box-shadow: $1 ;");
	}
}